package code.tools.commponents
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.structure.manager.font.FontManager;
	import code.tools.method.CommonMethod;
	import code.untils.UiNameConst;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class CheckBox extends Component
	{
		/** 复选框类型样式枚举：分享之类的 */
		public static const CHECKBOX_TYPE_SHARE:int = 10;
		/** 复选框类型样式枚举：人物名之类的 */
		public static const CHECKBOX_TYPE_ROLE_NAME:int = 20;
		/** 复选框类型样式枚举：裂变发送免费礼物之类的*/
		public static const CHECKBOX_TYPE_FISSION_FREE_GIFT:int = 30;
		protected var _label:TextField;
//		protected var _label:TextFlow;
//		private var _span:SpanElement;
		protected var _labelText:String = "";
		protected var _selected:Boolean = true;
		private var _clickHandler:Function;
		
		protected var checkBoxMc:MovieClip;
		private var _type:int;
		
		private var _alignWidth:int;
		private var _align:String;
		private var _xpos:Number;
		private var _labelFilters:Array;
		/**
		 * Constructor
		 * @param parent The parent DisplayObjectContainer on which to add this CheckBox.
		 * @param xpos The x position to place this component.
		 * @param ypos The y position to place this component.
		 * @param defaultHandler The event handling function to handle the default event for this component (click in this case).
		 * @param label String containing the label for this component.
		 * @param type 复选框样式，见本类枚举
		 * @param alignWidth 对齐的宽度范围，为-1表示居左，由xpos决定位置
		 * @param align 对齐方式，默认居左
		 * @param labelFilters 文字滤镜
		 */
		public function CheckBox(parent:DisplayObjectContainer, xpos:Number, ypos:Number, labelStr:String,
								 defaultHandler:Function = null,type:int = 10,alignWidth:int = -1,align:String = 'left',labelFilters:Array = null)
		{
			_type = type;
			_alignWidth = alignWidth;
			_align = align;
			_xpos = xpos;
			_labelFilters = labelFilters;
			super(parent, xpos, ypos);
			_clickHandler = defaultHandler;
			label = labelStr;
		}

		/**
		 * Initializes the component.
		 */
		override protected function init():void
		{
			setSize(150,20);
			super.init();
			buttonMode = true;
			useHandCursor = true;
			mouseChildren = false;
		}

		/**
		 * Creates the children for this component
		 */
		override protected function addChildren():void
		{
			super.addChildren();
			graphics.beginFill(0,0);
			graphics.drawRect(0,0,_width,_height);
			graphics.endFill();
//			_label = new TextFlow();
//			var paragraph:ParagraphElement = new ParagraphElement();
//			_span = new SpanElement();
//			paragraph.addChild(_span);
//			_label.addChild(paragraph);
//			var factory:TextFlowTextLineFactory = new TextFlowTextLineFactory();
//			factory.compositionBounds = new Rectangle(0, 0, 240, 25);
//			var textLayoutFormat:TextLayoutFormat = new TextLayoutFormat();
//			textLayoutFormat.fontFamily = FontManager.getInstance().embeddedFontBName;
			switch(_type)
			{
				case CHECKBOX_TYPE_SHARE:
//					textLayoutFormat.fontSize = 15;
//					textLayoutFormat.color = 0x999999;
					checkBoxMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'checkBox') as Class) as MovieClip; 
					_label = Helpers.FormatTextField(this,22,0,120,TextFieldAutoSize.LEFT,0x794828,FontManager.getInstance().embeddedFontAName,12);
					break;
				case CHECKBOX_TYPE_ROLE_NAME:
//					textLayoutFormat.fontSize = 12;
//					textLayoutFormat.color = 0;
					checkBoxMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'roleNameWithInvitFriends') as Class) as MovieClip; 
					_label = Helpers.FormatTextField(this,26,-1,150,TextFieldAutoSize.LEFT,0x666666,FontManager.getInstance().embeddedFontCName,14);
					break;
				case CHECKBOX_TYPE_FISSION_FREE_GIFT:
					checkBoxMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'fissionFreeGiftCheckBox') as Class) as MovieClip; 
					_label = Helpers.FormatTextField(this,41,9,150,TextFieldAutoSize.LEFT,0x1F80C5,FontManager.getInstance().embeddedFontBName,18);
					break;
			}
//			_label.hostFormat = textLayoutFormat;
			_label.mouseEnabled = false;
			_label.filters = _labelFilters;
//			factory.createTextLines(onCreateTextLines, _label);
//			function onCreateTextLines(myTextLine:TextLine):void {
//				addChild(myTextLine);
//			}
			addChild(checkBoxMc);
			addEventListener(MouseEvent.CLICK, onClick);
		}

		///////////////////////////////////
		// public methods
		///////////////////////////////////

		/**
		 * Draws the visual ui of the component.
		 */
		override public function draw():void
		{
			super.draw();
			if(_label)
			{
				if(_type == CHECKBOX_TYPE_ROLE_NAME)
				{
					if(CommonMethod.checkIsEnglishWord(_labelText))
						_label.embedFonts = true;
					else
						_label.embedFonts = false;
				}
				_label.text = _labelText == null ? '' : _labelText;
			}
			if(_alignWidth == -1 || _align == TextFieldAutoSize.LEFT)
				x = _xpos;
			else
			{
				switch(_align)
				{
					case TextFieldAutoSize.CENTER:
						x = _xpos + (_alignWidth - (_label.x + _label.width))*.5;
						break;
					case TextFieldAutoSize.RIGHT:
						x = _xpos + (_alignWidth - (_label.x + _label.width));
						break;
				}
			}
//			if(_span)
//				_span.text = _labelText;
//			_label.x = 15;
//			_label.y = (8 - _label.height) / 6;
//			_width = _label.x + _label.textWidth;
//			_height = 20;
		}

		///////////////////////////////////
		// event handler
		///////////////////////////////////

		/**
		 * Internal click handler.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onClick(event:MouseEvent):void
		{
			selected = !selected;
			if(_clickHandler == null)return
			_clickHandler(this);
		}

		///////////////////////////////////
		// getter/setters
		///////////////////////////////////

		/**
		 * Sets / gets the label text shown on this CheckBox.
		 */
		public function set label(str:String):void
		{
			_labelText = str;
//			invalidate();
			draw();
		}
		public function get label():String
		{
			return _labelText;
		}
//		public function set textColor(v:int):void
//		{
//			_label.textColor = v;
//		}
		/**
		 * Sets / gets the selected state of this CheckBox.
		 */
		public function set selected(s:Boolean):void
		{
			_selected = s;
			checkBoxMc.gotoAndStop(s ? 1 : 2);
		}
		public function get selected():Boolean
		{
			return _selected;
		}

		/**
		 * Sets/gets whether this component will be enabled or not.
		 */
		public override function set enabled(value:Boolean):void
		{
			super.enabled = value;
			mouseChildren = false;
		}
		
		private var _textcolor:uint;

		public function get textcolor():uint
		{
			return _textcolor;
		}

		public function set textcolor(value:uint):void
		{
			_textcolor = value;
			if(_label)
				_label.textColor = value;
		}

		public function get textFilters():Array
		{
			return _textFilters;
		}

		public function set textFilters(value:Array):void
		{
			_textFilters = value;
			_label.filters = value;
		}

		public function get alignWidth():int
		{
			return _alignWidth;
		}

		public function set alignWidth(value:int):void
		{
			_alignWidth = value;
			draw();
		}

		public function get align():String
		{
			return _align;
		}

		public function set align(value:String):void
		{
			_align = value;
			draw();
		}

		
		private var _textFilters:Array;
	}
}